<template>
  <div class="app-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item v-show="false" label="站点ID">
        <el-input v-model="ruleForm.webId"></el-input>
      </el-form-item>
      <el-form-item prop="webName" label="站点名称">
        <el-input v-model="ruleForm.webName"></el-input>
      </el-form-item>
      <el-form-item prop="webSite" label="站点网址">
        <el-input v-model="ruleForm.webSite"></el-input>
      </el-form-item>
      <el-form-item prop="webLogo" label="网站Logo">
        <el-upload
          v-loading="upLoading"
          class="avatar-uploader"
          :action="apiUrl+'/system/upload/upImg'"
          :show-file-list="false"
          name="file"
          :data="setUpData()"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="recordInfo" label="备案信息">
        <el-input type="textarea" v-model="ruleForm.recordInfo"></el-input>
      </el-form-item>
      <el-form-item prop="statisticsCode" label="代码统计">
        <el-input type="textarea" v-model="ruleForm.statisticsCode"></el-input>
      </el-form-item>
      <el-form-item label="版权信息">
        <el-input type="textarea" v-model="ruleForm.copyrightInfo"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getInfo, updateInfo } from '../../../../api/system/config/webset'
import {getToken} from '../../../../utils/auth'
// import qs from 'qs'
export default {
  name: 'index',
  data () {
    return {
      // 图片上传地址
      imageUrl: '',
      // 上传加载
      upLoading: false,
      // 表单参数
      ruleForm: {},
      // 表单验证
      rules: {
        webName: [
          {required: true, message: '站点名称不能为空', trigger: 'blur'}
        ],
        webSet: [
          {required: true, message: '站点网址不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created () {
    this.getWeb()
  },
  methods: {
    handleAvatarSuccess (res, file) {
      if (res.code === 0) {
        this.imageUrl = URL.createObjectURL(file.raw)
        this.ruleForm.webLogo = res.data.fileInfo.fileUrl
      } else {
        this.msgError(res.msg)
      }
      this.upLoading = false
    },
    beforeAvatarUpload (file) {
      this.upLoading = true
      return true
    },
    setUpData () {
      return {token: getToken()}
    },
    getWeb () {
      getInfo().then(response => {
        let data = response.data.webContent
        if (data) {
          const content = JSON.parse(data, this.ruleForm)
          if (content) {
            this.ruleForm = {
              webId: content.webId,
              webName: content.webName,
              webSite: content.webSite,
              webLogo: content.webLogo,
              recordInfo: content.recordInfo,
              statisticsCode: content.statisticsCode,
              copyrightInfo: content.copyrightInfo
            }
            this.imageUrl = this.apiUrl + '/' + this.ruleForm.webLogo
          }
        }
      })
    },
    submitForm (formName) {
      // const rule = this.ruleForm
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateInfo(this.ruleForm).then((response) => {
            if (response.data) {
              this.msgSuccess('修改保存成功')
            } else {
              this.msgError('修改保存失败')
            }
          })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.imageUrl = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
